package com.ling.pl.security.dao;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.security.model.DefaultRole;
import com.ling.pl.security.model.RoleMember;
import com.ling.pl.security.model.RoleResource;
import com.ling.pl.security.model.UrlComponent;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
@Component(DefaultRoleDao.BEAN_ID)
public class DefaultRoleDao extends BaseDefaultRoleDao {
    public List<DefaultRole> loadAllRoles() {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("FROM " + DefaultRole.class.getName() + " R order by sortOrder desc");
//		if(StringUtils.isNotEmpty(this.getFixedCompanyId())){
//			query.append(" where R.companyId=:companyId");
//			return getJdbcTemplate().query(sql,new Object[]{this.getFixedCompanyId()},new RoleRowMapper());
//		}else{
//			return getJdbcTemplate().query(sql, new RoleRowMapper());
//		}
        return findBy(query, values);
    }

    public DaoPage loadAllRoles(Map params, Integer pageIndex, Integer pageSize) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("from " + DefaultRole.class.getName() + " r where 1=1");
        query.append(" and r.type=:type and r.companyId=:companyId");
        values.put("type", params.get("type"));
        values.put("companyId", params.get("companyId"));
        query.append(" order by sortOrder  desc");
        return pageBy(query, values, pageIndex, pageSize);
    }

    public List<RoleMember> loadRoleMemberByRoleId(String roleId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT M FROM " + RoleMember.class.getName() + " M WHERE M.roleId=:roleId");
        values.put("roleId", roleId);
        return findBy(query, values);

    }

    public List<RoleMember> loadRoleMemberByRoleNameAndType(String rolename, String type) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT m FROM " + RoleMember.class.getName() + " m," + DefaultRole.class.getName() + " r WHERE m.roleId=r.id and r.name=:rolename ");
        values.put("rolename", rolename);
        buidlRoleMenberTypeQuery(type, query);
        query.append(" order by m.createDate desc");
        return findBy(query, values);
    }

    private void buidlRoleMenberQuery(String roleId, String type,
                                      StringBuffer query, Map values) {
        query.append("SELECT m FROM " + RoleMember.class.getName() + " m  WHERE m.roleId=:roleId ");
        values.put("roleId", roleId);
        buidlRoleMenberTypeQuery(type, query);
        query.append(" order by m.createDate desc");
    }

    private void buidlRoleMenberTypeQuery(String type, StringBuffer query) {
        if (type.equals("user")) {
            query.append(" and m.username is not null");
        } else if (type.equals("org")) {
            query.append(" and m.orgId is not null");
        } else if (type.equals("position")) {
            query.append(" and m.positionId is not null");
        } else if (type.equals("group")) {
            query.append(" and m.group is not null");
        }
    }

    public DaoPage loadRoleMemberByRoleIdAndType(String roleId, String type, Integer pageIndex, Integer pageSize) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        buidlRoleMenberQuery(roleId, type, query, values);
        return pageBy(query, values, pageIndex, pageSize);
    }

    public Integer loadRoleMemberSizeByRoleIdAndType(String roleId, String type, String id) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT count(rm) FROM " + RoleMember.class.getName() + " rm WHERE rm.roleId=:roleId");
        values.put("roleId", roleId);
        if (type.equals("user")) {
            values.put("username", id);
            query.append(" and rm.username=:username ");
        } else if (type.equals("org")) {
            values.put("orgId", id);
            query.append(" and rm.orgId=:orgId");
        } else if (type.equals("position")) {
            values.put("positionId", id);
            query.append(" and rm.positionId=:positionId");
        } else if (type.equals("group")) {
            values.put("groupId", id);
            query.append(" and rm.group.id=:groupId");
        }
        return queryForInt(query.toString(), values);
    }


    public List<RoleResource> loadRoleResourceByRoleId(String roleId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT M FROM " + RoleResource.class.getName() + " M WHERE M.roleId=:roleId");
        values.put("roleId", roleId);
        return findBy(query, values);
    }


    public List<UrlComponent> loadUrlComponentByRoleId(String roleId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT M FROM " + UrlComponent.class.getName() + " M WHERE M.roleId=:roleId");
        values.put("roleId", roleId);
        return findBy(query, values);
    }


    public List<DefaultRole> loadRolesByUserName(String userName) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("SELECT distinct(r) FROM " + DefaultRole.class.getName() + " r," + RoleMember.class.getName() + " m WHERE m.roleId=r.id ");
        query.append(" and m.username=:username");
        values.put("username", userName);
        query.append(" order by r.sortOrder desc");//不能更改排序方式,否则组件权限不能正确覆盖
        List<DefaultRole> results = findBy(query, values);
        return results;
    }

    class RoleRowMapper implements RowMapper<DefaultRole> {
        public DefaultRole mapRow(ResultSet rs, int rowNum) throws SQLException {
            DefaultRole role = new DefaultRole();
            role.setId(rs.getString("ID_"));
            role.setCompanyId(rs.getString("COMPANY_ID_"));
            return role;
        }
    }
}

